package com.dream.utils.email;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import com.dream.utils.log.DreamLogUtils;

/**load the email teamplate
 * @copyright evan
 * @author evan
 * @Revision
 * @date 2012/09/01
 */
public class EMailTeamplateXML {
	
	// email nodes
	public static class XMLElments {
		public static final String FILE_ANME = "EmailTeamplate.xml";
		public static final String TYPES = "types";
		public static final String TYPE = "type";
		public static final String SUBJECT = "subject";
		public static final String CONTENT = "content";
		public static final String SIGNATURE = "signature";
	}
	public static class Params{
		public static final String RECEIVE_NAME = "@name@";
		public static final String SIGNATURE_NAME = "@name@";
		public static final String SIGNATURE_MOBILE = "@mobile@";
		public static final String SIGNATURE_MAIL= "@mail@";
		public static final String NEW_PWD= "@newpwd@";
	}
	public static EMailBean MAIL =  new EMailBean();
	
	static{
		try{
			String path = EMailTeamplateXML.class.getClassLoader().getResource("").getPath()+XMLElments.FILE_ANME;
			path = java.net.URLDecoder.decode(path,"utf-8");
			File file = new File(path);
			if(file.exists()){
			FileInputStream inputStream = new FileInputStream(file);
				SAXParserFactory factory = SAXParserFactory.newInstance();
				factory.setValidating(true);
				XMLReader reader = factory.newSAXParser().getXMLReader();
				reader.setContentHandler(new MailTemplateXMLHandler());
				reader.parse(new InputSource(inputStream));
			}
		}catch(Exception e){
			DreamLogUtils.error(EMailTeamplateXML.class,"static","init exception",e);
			e.printStackTrace();
		}
	}
	static class MailTemplateXMLHandler  extends DefaultHandler{
		private String tagName;
		private List<EMailBean.MailType> mailTypes;
		private EMailBean.MailType mailType;
		private StringBuilder sb = new StringBuilder();
		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if(XMLElments.SIGNATURE.equals(tagName)){
				sb.append(new String(ch, start, length));
			}
			if(XMLElments.SUBJECT.equals(tagName)){
				mailType.setSubject(new String(ch, start, length));
			}
			
			if(XMLElments.CONTENT.equals(tagName)){
				sb.append(new String(ch, start, length));
			}
			
		}
	
		@Override
		public void endDocument() throws SAXException {
			System.out.println("end start read xml");
		}
	
		@Override
		public void startDocument() throws SAXException {
			MAIL = new EMailBean();
			System.out.println("begin start read xml");
		}
	
		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			tagName = qName;
			if(XMLElments.TYPES.equals(tagName)){
				mailTypes = new ArrayList<EMailBean.MailType>();
				MAIL.setMailTypes(mailTypes);
			}
			if(XMLElments.TYPE.equals(tagName)){
				mailType = new EMailBean.MailType();
				mailTypes.add(mailType);
			}
		}
	
		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			
			if(XMLElments.CONTENT.equals(qName)){
				mailType.setContent(sb.toString());
				sb.setLength(0);
			}
			
			if(XMLElments.SIGNATURE.equals(qName)){
				MAIL.setSignature(sb.toString());
				sb.setLength(0);
			}

		}
	}
	
	public static void main(String[] args){
		String signature = MAIL.getSignature();
		signature = signature.replaceFirst(EMailTeamplateXML.Params.SIGNATURE_MAIL, "1111");
		System.out.println(signature);
	}
}
